package controller;

import dao.CarDAOImpl;
import entity.Car;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/update_car")
public class UpdateCarServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("application/json;charset=utf8");
        resp.setCharacterEncoding("utf8");

        //获取打印输出流,向网页输出内容
        PrintWriter writer = resp.getWriter();
        String name = req.getParameter("name");
        String carName = req.getParameter("car_name");
        String date = req.getParameter("date");
        int cost =Integer.parseInt( req.getParameter("cost"));
        int id =Integer.parseInt( req.getParameter("id"));
        Car car = new Car(id,name,carName,cost,date);

        CarDAOImpl dao = new CarDAOImpl();
        int i = dao.updateCar(car);

        if (i>0){
            writer.print("修改成功");
        }else{
            writer.print("修改失败");
        }
        writer.close();
    }
}
